<?php

/**
 * google api package
 * 
 * 
 * @package php.ui.winbinder.google
 */


/**
 * WBGoogleChart
 * 
 * http://chart.apis.google.com/chart?cht=p3&chd=s:hW&chs=250x100&chl=Hello|World
 *
 * @package php.ui.winbinder.google
 */
class WBGoogleChart extends WBHTMLControl { 
	
	const CHART_URL = "http://chart.apis.google.com/chart";
	
	public function __construct($options = array()) { 
		parent::__construct($options);
	}
	
	public function initComponent(){
		$this->setOption('url', "about:blank");
		
		if (!isset($this->chartType)) $this->setOption('chartType', 'image');
		
		parent::initComponent();
	}
	
	public function onResize() { 
		parent::onResize();
		
		$this->renderChart();
	}
	
	public function renderChart() {
		if ($this->getOption('chartType') == 'image') { 		// Image charts 
			$url = $this->createUrl(); 
			$this->setLocation($url);
		} else { 												// Interactive Charts
			
		}
	}

	public function createUrl() { 
		return self::CHART_URL . "?" . $this->getParameter();
	}
	
	public function getParameter() { 
		$arr = array(
			'chs' => $this->getChartSize(),
		);
		
		$options = $this->getOptionList();
		
		foreach($options as $key => $value) { 
			if (strpos($key, 'ch') !== false && !in_list($value, 'chartType') ) { 
				$arr[$key] = $value;
			}
		}
		
		return http_build_query($arr);
	}
	
	public function getChartSize() { 
		$size = $this->getSize();

		if ($size['width'] > 1000) $size['width'] = 1000;
		if ($size['height'] > 1000) $size['height'] = 1000;
		
		if ($size['width'] * $size['height'] > 300000) { 
			if ($this->getOption('basis') == 'width') { 
				$size['height'] = (int)(300000 / $size['width']);
			} else { 
				$size['width'] = (int)(300000 / $size['height']);
			}
		}
		
		$size['width'] -= 50;
		$size['height'] -= 40;

		
		return $size['width']."x".$size['height'];
	}
}

?>
